package cn.tedu.luban.worker.main.controller;

import cn.tedu.luban.passport.protocol.jwt.UserPrinciple;
import cn.tedu.luban.passport.sdk.security.LubanSecurityContext;
import cn.tedu.luban.worker.main.converter.WorkerAuditConverter;
import cn.tedu.luban.worker.main.converter.WorkerConverter;
import cn.tedu.luban.worker.main.service.WorkerService;
import cn.tedu.luban.worker.protocol.VO.AdminAuditDetailVO;
import cn.tedu.luban.worker.protocol.VO.AdminWorkerListVO;
import cn.tedu.luban.worker.protocol.VO.FrontWorkerDetailVO;
import cn.tedu.luban.worker.protocol.bo.AdminAuditBO;
import cn.tedu.luban.worker.protocol.bo.AdminWorkerBO;
import cn.tedu.luban.worker.protocol.bo.FrontWorkerBO;
import cn.tedu.luban.worker.protocol.param.AuditSubmitParam;
import cn.tedu.luban.worker.protocol.param.WorkerRegistParam;
import cn.tedu.luban.worker.protocol.query.WorkerListQuery;
import com.alibaba.fastjson.JSON;
import com.tedu.inn.protocol.ListRecordTotalBO;
import com.tedu.inn.protocol.exception.BusinessException;
import com.tedu.inn.protocol.model.Result;
import com.tedu.inn.protocol.pager.PagerResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@Slf4j
public class WorkerController {
    @Autowired
    private WorkerService workerService;
    /**
     * http接口 师傅入驻申请
     * 接口层 http接口 写操作
     * 入参 : Param后缀参数类型
     * 出参 : 封装自增id的数据
     */
    @PostMapping("/worker/create")
    public Result<Long> workerCreate(
            @RequestBody WorkerRegistParam param
    )
            throws BusinessException {
        UserPrinciple loginToken = LubanSecurityContext.getLoginToken();
        System.out.println(loginToken);
        //1.调用业务层 入参param 出参workerId
        Long workerId=workerService.workerCreate(param);
        //2.包装Result对象 返回
        return new Result<>(workerId);
    }
    /**
     * 用户登录手机算APP 后 师傅详情查询接口
     */
    @Autowired
    private WorkerConverter workerConverter;
    @GetMapping("/worker/detail")
    public Result<FrontWorkerDetailVO> workerDetail()throws BusinessException{
        //1.读操作 读单个对象 业务层读出参是一个BO
        Long start;
        Long end;
        log.info("start:{}",start=new Date().getTime());
        FrontWorkerBO frontWorkerBO=workerService.workerDetail();
        log.info("end:{}",end=new Date().getTime());
        log.info("业务查询时间:{}",end-start);
        //2.转vo
        FrontWorkerDetailVO vo = workerConverter.bo2vo(frontWorkerBO);
        return new Result<>(vo);
    }
    /**
     * 后台管理 师傅审核列表
     */
    @PostMapping("/admin/worker/aduit")
    public Result<PagerResult<AdminWorkerListVO>> workerAuditList(
            @RequestBody WorkerListQuery query)throws BusinessException{
        //pagerResult作为分页数据返回 后端包装5个属性 pageNo pageSize list total lastPageIndex
        //pageNo pageSize 就在query对象
        //list分页数据 total总条数 需要业务查询
        //lastPageIndex 根据total pageSize计算的
        ListRecordTotalBO<AdminWorkerBO> boListTotal=workerService.workerAuditList(query);
        //装配一个pagerResult
        PagerResult<AdminWorkerListVO> page = workerConverter.bo2PagerResultVO(boListTotal, query);
        return new Result<>(page);
    }
    @Autowired
    private WorkerAuditConverter workerAuditConverter;
    /**
     * 师傅审核列表之后的某个师傅的审核详情查询
     */
    @PostMapping("/admin/worker/audit/detail")
    public Result<AdminAuditDetailVO> auditDetail(
            @RequestParam(name="workerId") Long userId)throws BusinessException{
        //1. 查询操作从业务层返回的总是BO
        AdminWorkerBO  bo=workerService.auditDetail(userId);
        //2. 转化器转化
        AdminAuditDetailVO vo = workerAuditConverter.bo2vo(bo);
        return new Result<>(vo);
    }
    /**
     * 师傅后台管理审核提交
     */
    @PostMapping("/admin/worker/audit/save")
    public Result<Long> auditSubmit(@RequestBody AuditSubmitParam param)
            throws BusinessException{
        //1. 调用业务层提交审核
        Long auditId=workerService.auditSubmit(param);
        //2. 封装auditId返回给前端
        return new Result<>(auditId);
    }
}
